//
//  SEVoiceFilesManager.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/5/6.
//

import Foundation

private let kChatVoiceAmrRecordFolder = "ChatVoiceAmrRecord" // 存 amr 的文件目录名
private let kChatVoiceWavRecordFolder = "ChatVoiceWavRecord" // 存 wav 的文件目录名

enum SEVoiceFileType: String {
    case amr, wav
}

class SEVoiceFilesManager {
    /// 返回 amr 完整路径
    /// - Parameter fileName: 文件名，不包含后缀
    /// - Returns: 返回路径
    class func recoderFilePath(with fileName: String, fileType: SEVoiceFileType) -> URL {
        let filePath = amrRecordFolder.appendingPathComponent("\(fileName).\(fileType.rawValue)")
        return filePath
    }
    
    /// 修改文件名称
    /// - Parameters:
    ///   - originPath:      原路径
    ///   - destinationPath: 目标路径
    /// - Returns: 目标路径
    class func renameFile(_ originPath: URL, destinationPath: URL) -> Bool {
        do {
            try FileManager.default.moveItem(atPath: originPath.path, toPath: destinationPath.path)
            return true
        } catch let error {
            SELog("error: \(error.localizedDescription)")
            return false
        }
    }
    
    /// 删除所有录音文件
    class func deleteAllRecordingFiles() {
        deleteRecordFiles(with: amrRecordFolder.path, fileType: .amr)
        deleteRecordFiles(with: wavRecordFolder.path, fileType: .wav)
    }
}

private extension SEVoiceFilesManager {
    /// 创建录音文件夹
    /// - Parameter folderName: 文件夹名
    /// - Returns: 返回路径
    class func createRecordFolder(_ folderName: String) -> URL {
        let fileManager = FileManager.default
        let documentsDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
        let folder = documentsDirectory.appendingPathComponent(folderName)
        if !fileManager.fileExists(atPath: folder.absoluteString) {
            do {
                try fileManager.createDirectory(atPath: folder.path, withIntermediateDirectories: true, attributes: nil)
                return folder
            } catch let error {
                SELog("error: \(error.localizedDescription)")
            }
        }
        return folder
    }
    
    /// amr 录音文件夹
    class var amrRecordFolder: URL {
        createRecordFolder(kChatVoiceAmrRecordFolder)
    }
    
    /// wav 录音文件夹
    class var wavRecordFolder: URL {
        createRecordFolder(kChatVoiceWavRecordFolder)
    }
    
    /// 删除录音文件
    /// - Parameter path: 路径
    class func deleteRecordFiles(with path: String, fileType: SEVoiceFileType) {
        let fileManager = FileManager.default
        do {
            let files = try fileManager.contentsOfDirectory(atPath: path)
            let recordings = files.filter { return $0.hasSuffix(fileType.rawValue) }
            for i in 0..<recordings.count {
                let path = path + "/" + recordings[i]
                SELog("删除文件: \(path)")
                do {
                    try fileManager.removeItem(atPath: path)
                } catch let error {
                    SELog("不能删除文件 \(path)")
                    SELog("error: \(error.localizedDescription)")
                }
            }
        } catch let error {
            SELog("在以下位置无法获取目录的内容 \(path)")
            SELog("error: \(error.localizedDescription)")
        }
    }
}
